package com.atbeijing.kafka.produce;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;

import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.Future;

public class Producer {

    public static void main(String[] args) {
        Properties conf = new Properties();
        try {
            //通过当前类获取类加载器,以流的方式加载配置文件
            conf.load(Producer.class.getClassLoader().getResourceAsStream("producer.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //泛型为消息的kv类型
        KafkaProducer<String, String> produce = new KafkaProducer<String, String>(conf);
        //发送消息
        Future<RecordMetadata> send = produce.send(new ProducerRecord<String, String>("first", "01", "01hello"));

        //关闭资源
        produce.close();
    }
}
